﻿@{
    Layout = "~/Views/Shared/_FormWhite.cshtml";
}

@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment HostingEnvironment
@section header{
    <link href="@Url.Content("~/lib/summernote/0.8.15/summernote.min.css")" rel="stylesheet" type="text/css">
    <script src='@Url.Content("~/lib/summernote/0.8.15/summernote.js")' type="text/javascript"></script>
    <script src='@Url.Content("~/lib/summernote/0.8.15/lang/summernote-zh-CN.min.js")' type="text/javascript"></script>

    @BundlerHelper.Render(HostingEnvironment.ContentRootPath, Url.Content("~/lib/imageupload/1.0/css/imgup.min.css"))
    @BundlerHelper.Render(HostingEnvironment.ContentRootPath, Url.Content("~/lib/imageupload/1.0/js/imgup.min.js"))

    @BundlerHelper.Render(HostingEnvironment.ContentRootPath, Url.Content("~/lib/bootstrap.tagsinput/0.8.0/bootstrap-tagsinput.min.css"))
    @BundlerHelper.Render(HostingEnvironment.ContentRootPath, Url.Content("~/lib/bootstrap.tagsinput/0.8.0/bootstrap-tagsinput.min.js"))
}


<div class="wrapper animated fadeInRight">
    <form id="form" class="form-horizontal m">
        <div class="form-group">
            <label class="col-sm-2 control-label ">商店<font class="red"> *</font></label>
            <div class="col-sm-10">
                <div style="width:100%;" id="shop_id" col="shop_id"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">品牌<font class="red"> *</font></label>
            <div class="col-sm-10">
                <div style="width:100%;" id="brand_id" col="brand_id"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">商品编号<font class="red"> *</font></label>
            <div class="col-sm-10">
                <input id="goods_core" col="goods_core" type="text" class="form-control" />
            </div>
        </div>
        @*<div class="form-group">
            <label class="col-sm-2 control-label ">商品类别<font class="red"> *</font></label>
            <div class="col-sm-10" id="category_id" col="category_id"></div>
        </div>*@
        @*<div class="form-group">
            <label class="col-sm-2 control-label ">商品型号<font class="red"> *</font></label>
            <div class="col-sm-10" id="goods_model_id" col="goods_model_id"></div>
        </div>*@
        <div class="form-group">
            <label class="col-sm-2 control-label ">商品名称<font class="red"> *</font></label>
            <div class="col-sm-10">
                <input id="goods_name" col="goods_name" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">商品价格<font class="red"> *</font></label>
            <div class="col-sm-10">
                <input id="price" col="price" type="text" value="1" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">单位</label>
            <div class="col-sm-10">
                <input id="unit" col="unit" type="text" value="1" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">
                <span>商品封面</span><font class="red"> *</font><br />
                <span>(宽高比 1.5：1)</span>
            </label>
            <div class="col-sm-10">
                <div id="goods_image" class="img-box">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">
                <span>商品图集</span><font class="red"> *</font><br />
                <span>(宽高比 1.5：1)</span>
            </label>
            <div class="col-sm-10">
                <div id="goods_images" class="img-box">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">是否推荐<font class="red"> *</font></label>
            <div class="col-sm-10">
                <div class="col-sm-4" id="is_master" col="is_master"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">是否上架<font class="red"> *</font></label>
            <div class="col-sm-10">
                <div class="col-sm-4" id="publish_status" col="publish_status"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">商品简介<font class="red"> *</font></label>
            <div class="col-sm-10">
                <div id="goods_brief" class="summernote"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">商品详情<font class="red"> *</font></label>
            <div class="col-sm-10">
                <div id="goods_intro" class="summernote"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">审核<font class="red"> *</font></label>
            <div class="col-sm-10">
                <div class="col-sm-4" id="audit_status" col="audit_status"></div>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    var id = ys.request("id");
    var shopid = ys.request("shopid");

    $(function () {

        $("#shop_id").ysComboBox({
            url: '@Url.Content("~/QixingManage/Store/GetListJson")' + '?id=' + shopid,
            key: "Id",
            value: "shop_name",
            class: "form-control",
        });

        @*$("#category_id").ysComboBox({
            url: '@Url.Content("~/QixingManage/GoodsCategory/GetListJson")',
            key: "Id",
            value: "category_name",
            class: "form-control",
        });*@

        //$("#unit").ysComboBox({
        //    data: ys.getJson([{"Key":1,"Value":"每/件"}]),
        //    class: "form-control"
        //});

         @*$("#goods_model_id").ysComboBox({
            url: '@Url.Content("~/QixingManage/Models/GetListJson")',
            key: "Id",
            value: "model_name",
            class: "form-control",
            onChange: function (val)
            {
                var goods_type=$("#goods_type").ysRadioBox('getValue');
                if (goods_type=="3") {
                    var seled=$('#goods_model_id').ysComboBox('getData');
                    var jsonseled = JSON.parse(seled[0].title);
                    if (!ys.isNullOrEmpty(jsonseled.rule)) {
                         $("#price").val(jsonseled.rule);
                    }
                    if (!ys.isNullOrEmpty(jsonseled.unit)) {
                         $("#unit").ysComboBox('setValue', jsonseled.unit);
                    }
                }
            }
        });*@




        $("#brand_id").ysComboBox({
            url: '@Url.Content("~/QixingManage/StoreBrand/GetStoreBrandList")'+ '?shopid=' + shopid,
            key: "Id",
            value: "brand_name",
            class: "form-control",
        });



        $("#is_master").ysRadioBox({ data: ys.getJson(@Html.Raw(typeof(GoodsIsMasterStatusEnum).EnumToDictionaryString())) });

        $("#publish_status").ysRadioBox({ data: ys.getJson(@Html.Raw(typeof(GoodsPublishStatusEnum).EnumToDictionaryString())) });

        $("#audit_status").ysRadioBox({ data: ys.getJson(@Html.Raw(typeof(GoodsAuditStatusEnum).EnumToDictionaryString())) });

        $("#goods_image").imageUpload({ uploadImage: 'uploadThumbImage', limit: 1, context: ctx });

        $("#goods_images").imageUpload({ uploadImage: 'uploadThumbImage', limit: 9, context: ctx });

        $('#goods_brief').summernote({
            height: '220px',
            lang: 'zh-CN',
            dialogsInBody: true,
            callbacks: {
                onImageUpload: function (files, editor, welEditable) {
                    uploadNewsImage(files[0], editor, welEditable,$('#goods_brief'));
                }
            }
        });

        $('#goods_intro').summernote({
            height: '220px',
            lang: 'zh-CN',
            dialogsInBody: true,
            callbacks: {
                onImageUpload: function (files, editor, welEditable) {
                    uploadNewsImage(files[0], editor, welEditable,$('#goods_intro'));
                }
            }
        });

        getForm();

        $('#form').validate({
            rules: {
                shop_id_select: { required: true },
                brand_id_select: { required: true },
                /*goods_type_select: { required: true },*/
                goods_core: { required: true },
                /*goods_model: { required: true},*/
                price: { required: true,decimalsValue: true},
                goods_name: { required: true },
                goods_brief: { required: true },
                goods_intro: { required: true }
            }
        });
    });

    function getForm() {
        if (id > 0) {
            ys.ajax({
                url: '@Url.Content("~/QixingManage/StoreGoods/GetFormJson")' + '?id=' + id,
                type: 'get',
                success: function (obj) {
                    if (obj.Tag == 1) {
                        $('#form').setWebControls(obj.Data);
                        $("#shop_id_select").select2({
                            "disabled": true
                        });
                        $("#goods_image").imageUpload("setImageUrl", obj.Data.goods_image);
                        $("#goods_images").imageUpload("setImageUrl", obj.Data.goods_images);
                        $("#goods_brief").summernote('code', obj.Data.goods_brief);
                        $("#goods_intro").summernote('code', obj.Data.goods_intro);
                        $("#goods_core").prop('readonly', true);
                    }
                }
            });
        }
        else {
            var defaultData = {};
            if (shopid != "null")
            {
                defaultData.shop_id = shopid;
                $("#shop_id_select").select2({
                    "disabled": true
                })
            }
            defaultData.audit_status ="@GoodsAuditStatusEnum.Yes.ParseToInt()";
            defaultData.publish_status = "@GoodsPublishStatusEnum.Yes.ParseToInt()";
            defaultData.is_master = "@GoodsIsMasterStatusEnum.No.ParseToInt()";
            $('#form').setWebControls(defaultData);
        }
    }

    function saveForm(index) {
        if ($('#form').validate().form()) {
            var postData = $('#form').getWebControls({ Id: id });

            //postData.category_name = $("#category_id").ysComboBox("getText");

            //postData.goods_model = $("#goods_model_id").ysComboBox("getText");

            postData.goods_image = $("#goods_image").imageUpload("getImageUrl");

            //if (ys.isNullOrEmpty(postData.goods_image))
            //{
            //    return ys.msgError("请上传商品封面!");
            //}

            //if (ys.isNullOrEmpty(postData.goods_model_id))
            //{
            //   return ys.msgError("请选择商品型号!");
            //}

            //postData.goods_model = $('#goods_model_id').ysComboBox('getText');

            postData.shop_name = $('#shop_id').ysComboBox('getText');

            postData.goods_images = $("#goods_images").imageUpload("getImageUrl");

            //if (ys.isNullOrEmpty(postData.goods_images))
            //{
            //   return ys.msgError("请上传商品图集!");
            //}

            postData.goods_brief = $("#goods_brief").summernote('code');
            postData.goods_intro = $("#goods_intro").summernote('code');
            ys.ajax({
                url: '@Url.Content("~/QixingManage/StoreGoods/SaveFormJson")',
                type: 'post',
                data: postData,
                success: function (obj) {
                    if (obj.Tag == 1) {
                        ys.msgSuccess(obj.Message);
                        if (id > 0) {
                            parent.searchPageGrid();
                        }
                        else
                        {
                            parent.searchGrid();
                        }

                        parent.layer.close(index);
                    }
                    else {
                        ys.msgError(obj.Message);
                    }
                }
            });
        }
    }
    function uploadNewsImage(file, editor, welEditable,$obj) {
        var formdata = new FormData();
        formdata.append("fileList", file);
        ys.ajaxUploadFile({
            url: '@GlobalContext.SystemConfig.ApiSite' + '/File/UploadFile?fileModule=@UploadFileType.Qixing.ParseToInt()',
            data: formdata,
            success: function (obj) {
                if (obj.Tag == 1)
                {
                    $obj.summernote('insertImage', '@GlobalContext.SystemConfig.ApiSite' + obj.Data, '/');
                }
                else {
                    ys.msgError(obj.Message);
                }
            }
        })
    }

    function uploadThumbImage(file, callback) {
        var formdata = new FormData();
        formdata.append("fileList", file);
        ys.ajaxUploadFile({
            url: '@GlobalContext.SystemConfig.ApiSite' + '/File/UploadFile?fileModule=@UploadFileType.Qixing.ParseToInt()',
            data: formdata,
            success: function (obj) {
                if (obj.Tag == 1) {
                    if (callback) {
                        callback('@GlobalContext.SystemConfig.ApiSite' + obj.Data);
                    }
                }
                else {
                    ys.msgError(obj.Message);
                }
            }
        })
    }
</script>

